package objects.delegates

import kotlin.properties.Delegates

interface A {

    fun say() {
        println("I'm, A")
    }
}

class Example(var a: A) : A by a {
    var p: String by Delegate()

    val lazy: String by Delegates.blockingLazy {
        println("computed!")
        "my lazy"
    }

    var name: String by Delegates.observable("fjay") {
        d, old, new ->
        println("$old - $new")
    }

    override fun toString() = "Example Class"
}

class Delegate() {
    fun get(thisRef: Any?, prop: PropertyMetadata): String {
        return "$thisRef, thank you for delegating '${prop.name}' to me!"
    }

    fun set(thisRef: Any?, prop: PropertyMetadata, value: String) {
        println("$value has been assigned to ${prop.name} in $thisRef")
    }
}

fun main(args: Array<String>) {
    val e = Example(object : A {
    })

    println(e.p)
    e.p = "NEW"

    println("lazy = ${e.lazy}")
    println("lazy = ${e.lazy}")

    e.name = "blue"

    e.say()
}
